import { connectMongoDB, disconnectMongoDB } from './mongodb';
import { testMysqlConnection, closeMysqlConnection } from './mysql';
import { logger } from '@/utils/logger';

/**
 * 初始化所有数据库连接
 */
export const initializeDatabases = async (): Promise<void> => {
  try {
    // 连接 MongoDB
    await connectMongoDB();
    
    // 测试 MySQL 连接
    const mysqlConnected = await testMysqlConnection();
    if (!mysqlConnected) {
      throw new Error('MySQL 连接失败');
    }
    
    logger.info('所有数据库连接初始化完成');
  } catch (error) {
    logger.error('数据库初始化失败:', error);
    throw error;
  }
};

/**
 * 关闭所有数据库连接
 */
export const closeDatabases = async (): Promise<void> => {
  try {
    await Promise.all([
      disconnectMongoDB(),
      closeMysqlConnection(),
    ]);
    
    logger.info('所有数据库连接已关闭');
  } catch (error) {
    logger.error('关闭数据库连接失败:', error);
  }
};

// 导出数据库模块
export * from './mongodb';
export * from './mysql';